package com.kuaimacode.kframework.code.services;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.common.ZtreeView;
import com.kuaimacode.kframework.constants.SysAdminPermissionConstants;
import com.kuaimacode.kframework.mybatis.mapper.admin.SysAdminMapRolePermissionMapper;
import com.kuaimacode.kframework.mybatis.mapper.admin.SysAdminPermissionMapper;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统所有权限
 **/
@Service
public class SysAdminPermissonService extends ServiceImpl<SysAdminPermissionMapper, SysAdminPermission> {

	
	
	@Autowired
	private SysAdminMapRolePermissionMapper sysAdminMapRolePermissionMapper;
	
	
	/**
	 * 新建权限
	 * @param permission
	 * @return
	 */
	public int createPermission(SysAdminPermission permission) {
		return baseMapper.insert(permission);
	}
	
	
	/**
	 * 根据用户查询其下面所有权限
	 *
	 * @param userId
	 * @return
	 */
	public List<SysAdminPermission> findPermissionListByUserId(String userId){
		return baseMapper.findPermissionListByUserId(userId);
	}

	/**
	 * 传入所有权限list，将其按照父子关系组装
	 * @param permiissionList
	 * @return
	 */
	public List<SysAdminPermission> formatChildPermission(List<SysAdminPermission> permiissionList){
		// 最后的结果
		List<SysAdminPermission> menuList = new ArrayList<SysAdminPermission>();
		// 先找到所有的一级菜单
		for(SysAdminPermission permission:permiissionList){
			if(SysAdminPermissionConstants.FIST_MENU ==  permission.getPermissionType()){
				menuList.add(permission);
			}
		}
		
		if(menuList.size() > 0) {
			// 为一级菜单设置子菜单，getChild是递归调用的
			for (SysAdminPermission menu : menuList) {
				menu.setChildList(getChild(menu.getPermissionId(), permiissionList));
			}
		}


		return menuList;
	}

	/**
	 * 递归查找子菜单
	 *
	 * @param id
	 *            当前菜单id
	 * @param rootMenu
	 *            要查找的列表
	 * @return
	 */
	private List<SysAdminPermission> getChild(Long id, List<SysAdminPermission> rootMenu) {
		// 子菜单
		List<SysAdminPermission> childList = new ArrayList<>();
		for (SysAdminPermission menu : rootMenu) {
			// 遍历所有节点，将父菜单id与传过来的id比较
			if (menu.getPermissionId() > 0) {
				if ( menu.getParentPermissionId() !=null &&  id==menu.getParentPermissionId() ) {
					childList.add(menu);
				}
			}
		}
		// 把子菜单的子菜单再循环一遍
		for (SysAdminPermission menu : childList) {
			// 没有url子菜单还有子菜单
			if ("javascript:void(0);".equalsIgnoreCase(menu.getUrl())) {
				// 递归
				menu.setChildList(getChild(menu.getPermissionId(), rootMenu));
			}
		}
		// 递归退出条件
		if (childList.size() == 0) {
			return null;
		}
		
		//System.out.println("childList::");
		//System.out.println(JSON.toJSONString(childList));
		
		return childList;
	}

	/**
	 * 根据用户主键，获取其权限下所有的menu，按照sort asc排序
	 * @param userId
	 * @return
	 */
	public List<SysAdminPermission> findMenuListByUserId(String userId) {
		return  baseMapper.findMenuListByUserId(userId);
	}

	/**
	 *  分页查找所有的用户
	 * @param pageNum 当前页码，从1开始
	 * @param pageSize  一页显示多少行
	 * @param orderBy  排序字段+排序方式
	 * @return
	 */
	public PageInfo<SysAdminPermission> findAll(int pageNum, int pageSize,String orderBy){
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy(orderBy);
		List<SysAdminPermission> results = baseMapper.selectList(null);
		return new PageInfo<SysAdminPermission>(results);
	}

	/**
	 * 获取角色的权限树
	 *
	 * @param roleId
	 * @return
	 */
	public List<ZtreeView> tree(String roleId) {
		List<ZtreeView> resulTreeNodes = new ArrayList<ZtreeView>();
		List<String> permissionIds = sysAdminMapRolePermissionMapper.findAllPermissionIds(roleId);
		ZtreeView node;
		List<SysAdminPermission> allPermissions = baseMapper.findAllByOrderByParentAscIdAscSortAsc();
		for (SysAdminPermission resource : allPermissions) {
			node = new ZtreeView();
			node.setId(resource.getPermissionId().toString());
			if (resource.getParentPermissionId() == null) {
				node.setId(resource.getPermissionId().toString());
			} else {
				node.setPId(resource.getParentPermissionId().toString());
			}
			node.setName(resource.getPermissionName());
			if (permissionIds != null && permissionIds.contains(resource.getPermissionId().toString())) {
				node.setChecked(true);
			}
			resulTreeNodes.add(node);
		}
		return resulTreeNodes;
	}

	/**
	 * 获取最后插入的Id
	 *
	 * @return
	 */
	public Long getLastInsertId(){
		return baseMapper.getLastInsertId();
	}
}
